package net.zjitc.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import net.zjitc.entity.Category;
import net.zjitc.service.CategoryService;
import net.zjitc.mapper.CategoryMapper;
import net.zjitc.utils.JwtHelper;
import net.zjitc.utils.Result;
import net.zjitc.utils.ResultCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author win10
* @description 针对表【vueshop_category】的数据库操作Service实现
* @createDate 2024-12-20 15:26:19
*/
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category>
    implements CategoryService{
    @Autowired
    private JwtHelper jwtHelper;
    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public Result getCategoryList() {
        List<Category> list = this.list();
        String server="http://localhost:8088/";
        list.forEach(item->item.setPicture(StringUtils.isEmpty(item.getPicture())?"":server+item.getPicture()));
        return Result.build(list,200,"获取分类列表成功");
    }

    @Override
    public Result getCategoryById(Integer id) {
        Category category = categoryMapper.selectById(id);
        return Result.build(category,200,"获取分类列表成功");
    }

    @Override
    public Result addCategory(Category category) {
        int insert = categoryMapper.insert(category);
        if(insert>0){
            return Result.build(category,200,"添加分类成功");
        }
        return Result.build(null, ResultCodeEnum.ADD_ERROR);
    }

    @Override
    public Result saveCategory(Category category) {
        int updateById = categoryMapper.updateById(category);
        if(updateById>0){
            return Result.build(category,200,"修改分类成功");
        }
        return Result.build(null, ResultCodeEnum.UPDATE_ERROR);
    }

    @Override
    public Result delCategory(Integer id) {
        int i = categoryMapper.deleteById(id);
        if(i>0){
            return Result.build(null,200,"删除分类成功");
        }
        return Result.build(null, ResultCodeEnum.DELETE_ERROR);
    }
}




